﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DAL;
using System.Data;

namespace BLL
{
  public class ArticleBLL
  {
    //Get all

    public List<Article> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    //Get by Random 
    public DataTable GetRandom(int id,int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var list = data.Article_GetRandom(id);
        DataTable table = new DataTable();
        table.Columns.Add("Id");
        table.Columns.Add("Name");
        table.Columns.Add("Name_En");
        table.Columns.Add("Image");
        table.Columns.Add("CreateDate");
        int i = 1;
        foreach (var item in list)
        {
          if (i > top) break;
          DataRow row = table.NewRow();
          row["Id"] = item.Id;
          row["Name"] = item.Title;
          row["Name_En"] = item.Title_En;
          row["Image"] = item.Image;
          row["CreateDate"] = item.CreateDate.ToString("d-M-yyyy");
          table.Rows.Add(row);
          i++;
        }
        return table;
      }
    }   

    //Get by category
    public List<Article> GetByCategory(int idCategory)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByCategory(int cateId, bool isEnable, int page, int size, out int countItem)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            int skip = (page - 1) * size;
            List<Article> list = data.Articles.Where(p => p.CategoryId == cateId && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
            countItem = list.Count;
            return list.Skip(skip).Take(size).ToList();
        }
    }

    public List<Article> GetByCategory(int idCategory, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory).OrderByDescending(p => p.CreateDate).Skip(0).Take(top).ToList();
      }
    }

    public List<Article> GetByCategory(int idCategory, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByCategoryDescending(int idCategory, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == idCategory && p.isEnable == isEnable).OrderBy(p => p.CreateDate).ToList();
      }
    }

    //Get by page
    public List<Article> GetByPage(List<Article> list, int page, int size)
    {
      page--;
      int skip = page * size;
      return list.Skip(skip).Take(size).ToList();
    }

    //Get by Tag
    public List<Article> GetByTag(string strTag)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Tag.Contains(strTag)).OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Article> GetByTag(string strTag, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Tag.Contains(strTag) && p.isEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }
   
    public List<Article> GetByView(int top)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Articles.Where(p => p.isEnable == true).OrderByDescending(p => p.View).Take(top).ToList();
        }
    }

    public List<Article> GetByView(int notCateId, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId != notCateId && p.isEnable == true).OrderByDescending(p => p.View).Take(top).ToList();
      }
    }

    //Get by id
    public Article GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Article GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    public List<Article> GetByHot(bool isHot)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.IsHot == isHot && p.isEnable == true).ToList();
      }
    }

    public List<Article> GetByHot(bool isHot, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.IsHot == isHot && p.isEnable == true).Take(top).ToList();
      }
    }
    public List<Article> GetByNew(int top, int notCateId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.isEnable == true && p.CategoryId != notCateId).Take(top).ToList();
      }
    }
    public List<Article> GetByNew(int cateId, int top, int notCateId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Articles.Where(p => p.CategoryId == cateId && p.isEnable == true && p.CategoryId != notCateId).Take(top).ToList();
      }
    }

    //Enable
    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add View
    public void AddView(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        obj.View = obj.View + 1;

        data.SubmitChanges();
      }
    }

   //Add
    public void Add(Article obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Articles.InsertOnSubmit(obj);

        data.SubmitChanges();
      }
    }

   //Update
    public void Update(Article obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article article = data.Articles.Where(p => p.Id == obj.Id).FirstOrDefault();

        article.CategoryId = obj.CategoryId;
        article.Title = obj.Title;
        article.Title_En = obj.Title_En;
        article.Content = obj.Content;
        article.Content_En = obj.Content_En;
        article.CreateDate = obj.CreateDate;
        article.Description = obj.Description;
        article.Description_En = obj.Description_En;
        article.Image = obj.Image;
        article.isEnable = obj.isEnable;
        article.Tag = obj.Tag;
        article.View = obj.View;
        article.IsHot = obj.IsHot;

        data.SubmitChanges();
      }
    }

    //Delete
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Article obj = data.Articles.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Articles.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }

    public void DeleteByCate(int idCategory)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Article> obj = data.Articles.Where(p => p.CategoryId == idCategory).ToList();
        if (obj != null)
        {
          data.Articles.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
